<template>
  <div>
    <el-upload :action="dataObj.host" :data="dataObj" list-type="picture" :multiple="false" :show-file-list="showFileList"
      :file-list="fileList" :before-upload="beforeUpload" :on-remove="handleRemove" :on-success="handleUploadSuccess"
      :on-preview="handlePreview">
      <span style="" title="只能上传jpg/png文件，且不超过10MB">修改头像</span>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="fileList[0].url" alt="">
    </el-dialog>
  </div>
</template>
<script>
import { policy } from './policy'
import getUUID from '../../utils/uuid'

export default {
  name: 'singleUpload',
  props: {
    value: String
  },
  computed: {
    imageUrl() {
      return this.value;
    },
    imageName() {
      if (this.value != null && this.value !== '') {
        return this.value.substr(this.value.lastIndexOf("/") + 1);
      } else {
        return null;
      }
    },
    fileList() {
      return [{
        name: this.imageName,
        url: this.imageUrl
      }]
    },
    showFileList: {
      get: function () {
        return this.value !== null && this.value !== '' && this.value !== undefined;
      },
      set: function (newValue) {
      }
    }
  },
  data() {
    return {
      dataObj: {
        policy: '',
        signature: '',
        key: '',
        ossaccessKeyId: '',
        dir: '',
        host: '',
        // callback:'',
      },
      dialogVisible: false,
      headerVo: {
        id: '',
        header: ''
      },
      memberVo: [],
    };
  },
  methods: {
    // 更新头像
    updateHeader() {
      this.$http({
        url: this.$http.adornUrl("/shitou-member/shitoumember/member/updateHeader"),
        method: 'post',
        data: this.$http.adornData(this.headerVo)
      }).then(({ data }) => {
        if (data.code == 200) {
          this.$router.go(0)
        } else {
          this.$message({
            type: 'warning',
            message: data.message
          })
        }
      })
    },
    emitInput(val) {
      this.$emit('input', val)
    },
    handleRemove(file, fileList) {
      this.emitInput('');
    },
    handlePreview(file) {
      this.dialogVisible = true;
    },
    beforeUpload(file) {
      let _self = this;
      return new Promise((resolve, reject) => {
        policy().then(response => {
          // console.log("响应的数据",response);
          _self.dataObj.policy = response.data.policy;
          _self.dataObj.signature = response.data.signature;
          _self.dataObj.ossaccessKeyId = response.data.accessId;
          _self.dataObj.key = response.data.dir + getUUID() + '_${filename}';
          _self.dataObj.dir = response.data.dir;
          _self.dataObj.host = response.data.host;
          // console.log("响应的数据222。。。",_self.dataObj);
          resolve(true)
        }).catch(err => {
          reject(false)
        })
      })
    },
    handleUploadSuccess(res, file) {
      // console.log("上传成功...")
      this.showFileList = true;
      this.fileList.pop();
      this.fileList.push({ name: file.name, url: this.dataObj.host + '/' + this.dataObj.key.replace("${filename}", file.name) });
      this.headerVo.header = this.dataObj.host + '/' + this.dataObj.key.replace("${filename}", '') + file.name;
      this.memberVo.header = this.headerVo.header;
      localStorage.setItem("userData", JSON.stringify(this.memberVo));

      this.emitInput(this.fileList[0].url);
      this.updateHeader()
    }
  },
  async created() {
    let member = await this.$memberInfo()
    if (member !== null) {
      this.headerVo.id = member.id;
      this.memberVo = member;
    }
  }
}
</script>
<style></style>


